
export default class EventList {




    private list: { handler: Function, target: Object }[] = [];

    on(handler: Function, target: Object) {
        this.list.push({ handler: handler, target: target })
    }


    has(callback, target): number {
        for (let index = 0; index < this.list.length; index++) {
            const element = this.list[index];
            if (element.handler === callback && element.target === target) {
                return index;
            }
        }
        return -1;
    }

    size(): number {
        return this.list.length;
    }

    off(callback: Function, target: Object) {

        for (let index = 0; index < this.list.length; index++) {
            const element = this.list[index];
            if (element.handler === callback && element.target === target) {
                this.list.splice(index, 1);
                return;
            }
        }

    }

    emit(data?: any, data2?: any, data3?) {
        this.list.forEach(element => {
            // cc.log(' eventList data ==============',data)
            element.handler.call(element.target, data, data2, data3);
        });

    }

    offAll() {
        this.list.length = 0;
    }


}
